package com.kkb.service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kkb.mapper.GameMapper;
import com.kkb.mapper.GametypeMapper;
import com.kkb.mapper.TeamMapper;
import com.kkb.pojo.Game;
import com.kkb.pojo.Gametype;
import com.kkb.pojo.Team;
import com.kkb.pojo.TeamExample;
import com.kkb.vo.GameQueryVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class GameService {
    @Resource
    private TeamMapper teamMapper;
    @Resource
    private GameMapper gameMapper;
    @Resource
    private GametypeMapper gametypeMapper;
    /**
     * 根据球队名称进行查询，如果查找到则根据id进行填充,缺点是：和下面的一个方法重复了！
     */
    public Integer queryByName(String chineseName){
        TeamExample example = new TeamExample();
        TeamExample.Criteria criteria = example.createCriteria();
        criteria.andChineseNameEqualTo(chineseName);
        List<Team> teams = teamMapper.selectByExample(example);
        for (Team team : teams) {
            if (team!=null){
                return team.getTeamId();
            }
        }
        return null;
    }
    /**
     * 根据条件去查询比赛信息
     */
    public PageInfo<Game> queryByPage(Integer pageNum, Integer pageSize, GameQueryVo vo){
        List<Game> games = null;
        if (vo!=null){
            Integer teamId = queryByName(vo.getChineseName().trim());
            PageHelper.startPage(pageNum,pageSize);
            games = gameMapper.selectByMyWriter(teamId, vo.getBeginDate(), vo.getEndDate(), vo.getTypeId());
            for (Game game : games) {
                //给game添加一个自家球队，用来在页面显示球队的中文名称
                Integer homeTeamId = game.getHomeTeamId();
                Team team = teamMapper.selectByPrimaryKey(homeTeamId);
                game.setHomeTeam(team);
                //给game添加一个对家的球队，用来在页面显示球队的中文名称
                Integer visitingTeamId = game.getVisitingTeamId();
                Team team1 = teamMapper.selectByPrimaryKey(visitingTeamId);
                game.setVisitingTeam(team1);
                //给game添加一个比赛类型，用来在页面实现中文的比赛类型
                Integer typeId = game.getTypeId();
                Gametype gametype = gametypeMapper.selectByPrimaryKey(typeId);
                game.setGameType(gametype);
            }
        }
        return new PageInfo<>(games);
    }
    /**
     * 通过球队的中文名称找一个球队，缺点:和上面的一个方法重复了
     */
    public Integer queryChineseName(String chineseName){
        TeamExample example = new TeamExample();
        TeamExample.Criteria criteria = example.createCriteria();
        if (chineseName!=null && !"".equals(chineseName.trim())){
            criteria.andChineseNameEqualTo(chineseName);
        }
        List<Team> teams = teamMapper.selectByExample(example);
        for (Team team : teams) {
            if (team!=null){
                return team.getTeamId();
            }
        }
        return null;
    }
    /**
     * 去增加一条比赛信息
     */
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = {Exception.class})
    public int addGame(Game game){
        return gameMapper.insertSelective(game);
    }
    /**
     *通过比赛id去查询一条
     */
    @Transactional(propagation = Propagation.REQUIRED,readOnly = true)
    public Game queryById(Integer gameId){
        Game game = gameMapper.selectByPrimaryKey(gameId);
        //给homeTeam设置上值
        Integer homeTeamId = game.getHomeTeamId();
        Team team = teamMapper.selectByPrimaryKey(homeTeamId);
        game.setHomeTeam(team);
        //给visitingTeam设置值
        Integer visitingTeamId = game.getVisitingTeamId();
        Team team1 = teamMapper.selectByPrimaryKey(visitingTeamId);
        game.setVisitingTeam(team1);
        //给gameType设置上值
        Integer gameId1 = game.getGameId();
        Gametype gametype = gametypeMapper.selectByPrimaryKey(gameId1);
        game.setGameType(gametype);
        return game;
    }
    /**
     * 更新一条比赛记录
     */
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = {Exception.class})
    public int updateGame(Game game){
        return gameMapper.updateByPrimaryKeySelective(game);
    }
    /**
     * 根据id去删除一条比赛记录
     */
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = {Exception.class})
    public int deleteGame(Integer deleteId){
        return gameMapper.deleteByPrimaryKey(deleteId);
    }

}
